package org.piaohao.interceptor;

import com.jfinal.aop.PrototypeInterceptor;
import com.jfinal.core.ActionInvocation;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.piaohao.controller.DriverController;

/**
 *
 * @author piaohao
 */
public class DriverCondition extends PrototypeInterceptor {

    private static String[] order = {"compose_id desc, sort_index asc", "compose_id asc, sort_index asc", "final_price asc, sort_index desc", "study_time asc, sort_index desc"};
    private Integer start;
    private Integer limit;
    private String name;
    private String startDate;
    private String endDate;
    private Integer startAge;
    private Integer endAge;
    private String orgId;
    private DriverController c;

    /**
     * 对Condition的属性进行赋值初始化
     */
    private void init(ActionInvocation ai) throws Exception {
        c = (DriverController) ai.getController();
        start = c.getParaToInt("start");
        limit = c.getParaToInt("limit");
        name = c.getPara("name");
        startDate = c.getPara("startDate");
        endDate = c.getPara("endDate");
        if (c.getPara("startAge") != null && !c.getPara("startAge").equals("")) {
            startAge = c.getParaToInt("startAge");
        }
        if (c.getPara("endAge") != null && !c.getPara("endAge").equals("")) {
            endAge = c.getParaToInt("endAge");
        }
        orgId = c.getPara("orgId");
    }

    @Override
    public void doIntercept(ActionInvocation ai) {
        try {
            init(ai);
            c.dc = this;
            ai.invoke();
        } catch (Exception ex) {
            Logger.getLogger(DriverCondition.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 生成KlassController.list()的查询条件
     */
    public void buildSql(StringBuilder sql, List<Object> paras) {
        sql.append("from t_driver d left join t_organization o on d.org_id=o.id where 1 = 1");
        if (name != null && !name.equals("")) {
            sql.append(" and d.name like ?");
            paras.add("%" + name + "%");
        }
        if (startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("")) {
            sql.append(" and d.birthday between ? and ?");
            paras.add(startDate);
            paras.add(endDate);
        }
        if (startAge != null && startAge != 0 && !startAge.equals("") && endAge != null && endAge != 0 && !endAge.equals("")) {
            sql.append(" and d.age between ? and ?");
            paras.add(startAge);
            paras.add(endAge);
        }
        if (orgId != null && !orgId.equals("")) {
            sql.append(" and d.org_id=?");
            paras.add(orgId);
        }
    }

    public int getPageNum() {
        return start;
    }

    public int getPageSize() {
        return limit;
    }

    private void getPara(String startAge) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
